﻿/*
   绑定视图：GroupView.xaml
   文件说明：控制用户权限
*/

using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZFS.Library;
using ZFS.Model;
using ZFSData;
using ZFSData.InterFace.User;
using ZFSDomain.Common.CoreLib;
using ZFSDomain.Common.CoreLib.Helper;
using ZFSDomain.Common.CoreLib.Menu;
using ZFSDomain.Common.Domain;
using ZFSDomain.Interface.User;
using ZFSDomain.Service;
using ZFSDomain.SysModule;

namespace ZFSDomain.ViewModel.Group
{

    public class GroupViewModel : BaseOperation<tb_Group>
    {
        #region 属性

        /// <summary>
        /// 模块管理器
        /// </summary>
        private MenuManager _MenuManager;

        public MenuManager MenuManager
        {
            get { return _MenuManager; }
        }

        /// <summary>
        /// 菜单列表
        /// </summary>
        public List<MenuGroupModel> MenuGroupModels { get; set; }

        #endregion

        public override void InitViewModel()
        {
            try
            {
                _MenuManager = new MenuManager();
                _MenuManager.LoadModules();
                base.InitViewModel();
            }
            catch (Exception ex)
            {
                Msg.Error(ex);
            }
        }

        /// <summary>
        /// 新增用户组
        /// </summary>
        public override void Add<TModel>(TModel model)
        {
            try
            {
                GroupDialogViewModel view = new GroupDialogViewModel();
                view.IsReadOnly = false;
                view.Group = new tb_Group();
                view.MenuGroupModels = MenuManager.ModuleGroups;
                var funcList = ZFSBridge.BridgeFactory.GetGroupBridge().GetGroupFuncs(view.Group.GroupCode); //获取组权限数据
                view.CaCheUser = new List<View_GroupUser>();
                var dialog = ServiceProvider.Instance.Get<IGroupDialog>();
                dialog.BindViewModel(view);
                var result = dialog.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    GridModelList.Add(view.Group);
                }
            }
            catch (Exception ex)
            {
                Msg.Error(ex);
            }
        }

        /// <summary>
        /// 编辑用户组
        /// </summary>
        public override void Edit<TModel>(TModel CurrentGroup)
        {
            if (!this.GetButtonAuth(Authority.EDIT)) return;

            if (CurrentGroup != null)
            {
                try
                {
                    GroupDialogViewModel view = new GroupDialogViewModel();
                    view.IsReadOnly = true;
                    view.Group = ClassOperation.CopyByReflect(CurrentGroup as tb_Group);
                    view.MenuGroupModels = new List<MenuGroupModel>(MenuManager.ModuleGroups.ToArray());
                    var funcList = ZFSBridge.BridgeFactory.GetGroupBridge().GetGroupFuncs(view.Group.GroupCode); //获取组权限数据

                    #region Binding GroupAuthValue

                    funcList.ForEach(t =>
                    {
                        view.MenuGroupModels.ForEach(q =>
                        {
                            q.Nodes.ForEach(w =>
                            {
                                if (w.MenuCode.Equals(t.MenuCode))
                                {
                                    w.IsChecked = true;
                                    w.Nodes.ForEach(s =>
                                    {
                                        if ((t.Authorities & s.AuthValue) == s.AuthValue)
                                            s.IsChecked = true;
                                    });
                                }
                            });
                            if (q.Nodes.Where(x => x.IsChecked.Equals(true)).Count() == q.Nodes.Count)
                            {
                                q.IsChecked = true;
                            }
                        });
                    });

                    #endregion

                    view.CaCheUser = ZFSBridge.BridgeFactory.GetGroupBridge().GetGroupUsers(view.Group.GroupCode); //获取用户组
                    var dialog = ServiceProvider.Instance.Get<IGroupDialog>();
                    dialog.BindViewModel(view);
                    var result = dialog.ShowDialog();
                    if (result.HasValue && result.Value)
                    {
                        var EditGroup = GridModelList.FirstOrDefault(t => t.isid.Equals(view.Group.isid));
                        EditGroup.GroupName = view.Group.GroupName;
                    }
                }
                catch (Exception ex)
                {
                    Msg.Error(ex);
                }
            }
        }

        /// <summary>
        /// 删除用户组
        /// </summary>
        public override async void Del<TModel>(TModel CurrentGroup)
        {
            if (!this.GetButtonAuth(Authority.DELETE)) return;

            if (CurrentGroup != null)
            {
                try
                {
                    tb_Group group = CurrentGroup as tb_Group;
                    if (Msg.Question(string.Format("确认删除组:{0}?", group.GroupName)))
                    {
                        bool result = await Task.Run(() => ZFSBridge.BridgeFactory.GetGroupBridge().Remove(group.isid));
                        if (result)
                        {
                            var gp = GridModelList.FirstOrDefault(t => t.isid.Equals(group.isid));
                            GridModelList.Remove(gp);
                            Msg.Info("删除成功!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Msg.Error(ex);
                }
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="pageIndex"></param>
        public override async void GetPageData(int pageIndex)
        {
            try
            {
                var GroupList = await Task.Run(() => ZFSBridge.BridgeFactory.GetGroupBridge().GetGroups(SearchText));
                GridModelList.Clear();
                GroupList.ForEach((arg) => GridModelList.Add(arg));
            }
            catch (Exception ex)
            {
                Msg.Error(ex);
            }
        }
    }
}
